package component.global.manager
{
	import component.controls.loader.BaseLoader;
	import component.controls.loader.BitmapLoader;
	import component.controls.loader.LoaderEvent;
	import component.controls.loader.TextLoader;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.Dictionary;
	
	public class LoaderManager extends EventDispatcher
	{
		public static const MAX_LOADING_COUNT:int = 5;
		public function LoaderManager()
		{
			_loaderDic = new Dictionary();
			_waitingList = new Vector.<BaseLoader>();
			_loadingList = new Vector.<BaseLoader>();
		}
		
		private static var _instance:LoaderManager;
		
		public static function get Instance():LoaderManager{
			if(_instance == null){
				_instance = new LoaderManager();
			}
			return _instance;
		}
		
		private var _waitingList:Vector.<BaseLoader>;
		private var _loadingList:Vector.<BaseLoader>;
		private var _loaderDic:Dictionary;
		
		public function createAndStartLoader(url:String, type:String, variables:URLVariables = null, method:String = URLRequestMethod.GET):*{
			var loader:BaseLoader = createLoader(url,type,variables,method);
			startLoad(loader);
			return loader;
		}
		
		public function getLoaderByUrl(url:String):BaseLoader{
			return _loaderDic[url];
		}
		
		public function createLoader(url:String, type:String, variables:URLVariables = null, method:String = URLRequestMethod.GET):*{
			if(_loaderDic[url] == null){
				switch(type){
					case BaseLoader.BINARY_LOADER:
						_loaderDic[url] = new BaseLoader(url,variables,method);
						break;
					case BaseLoader.TEXT_LOADER:
						_loaderDic[url] = new TextLoader(url,variables,method);
						break;
					case BaseLoader.BITMAP_LOADER:
						_loaderDic[url] = new BitmapLoader(url,null, variables,method);
						break;
				}
			}
			return _loaderDic[url];
		}
		
		/**
		 * 执行 loader 的加载方法
		 * @param loader 
		 * 
		 */		
		public function startLoad(loader:BaseLoader):void{
			if(loader == null)return;
			if(loader.isSuccess){
				loader.dispatch(LoaderEvent.COMPLETE);
				return;
			}
			if(loader.isLoading)return;
			if(_loadingList.length > MAX_LOADING_COUNT){
				_waitingList.push(loader);
				return;
			}
			EventMgr.Instance.addEvent(loader,[LoaderEvent.COMPLETE, LoaderEvent.ERROR],[__onLoaderComplete, __onLoaderError]);
			if(_loadingList.indexOf(loader) < 0){
				_loadingList.push(loader);
			}
			loader.load();
		}
		
		private function __onLoaderError(e:LoaderEvent):void{
			trace("Load Error Url:" + e.loader.url);
		}
		
		private function __onLoaderComplete(e:LoaderEvent):void{
			if(e.loader.isComplete){
//				trace("Load Complete Url:" + e.loader.url);
				EventMgr.Instance.addEvent(e.loader,[LoaderEvent.COMPLETE],[__onLoaderComplete]);
				_loadingList.splice(_loadingList.indexOf(e.loader),1);
				loadNext();
			}
		}
		
		private function loadNext():void{
			if(_waitingList.length > 0){
				if(_loadingList.length < MAX_LOADING_COUNT){
					startLoad(_waitingList.shift());
				}
			} else {
				trace("_loadingList.length ============================== " + _loadingList.length.toString());
				if(_loadingList.length == 0){
					trace("LoaderManager stop.");
					dispatchEvent(new LoaderEvent(LoaderEvent.QUEUE_COMPLETE));
				}
			}
		}
	}
}